// Для того чтобы добавить нового оператора нужно:
// 1. добавить описание в массив opsoses
// 2. добавить обработку картинки если она нужна. Иначе описать заглушку. 
// 3. добавить обработку отсылки сообщения.
var opsoses = Array();


opsoses[opsoses.length] = {
	id		: 'megafonsib',
	name	: 'Мегафон-Сибирь',
	prefix	: ['7923', '7929', '7933'],
	icon	: 'megafon-icon.png',
	imgclb	: 'opsosList.getCodeMegafon',
	sendclb	: 'opsosList.sendMegafon',
	error	: '<p><font color=red>'
};
opsoses[opsoses.length] = {
	id		: 'tele2',
	name	: 'TELE2',
	prefix	: ['7902', '7904', '7908', '7950', '7951', '7952', '7953'],
	icon	: 'tele2-icon.png',
	imgclb	: 'opsosList.getCodeTele2',
	sendclb	: 'opsosList.sendTele2',
	ok : 'Спасибо, Ваше сообщение принято!'
	//error	: 'Вы ввели неверный код'
};
opsoses[opsoses.length] = {
	id		: 'megafonvolga',
	name	: 'Мегафон-Поволжье',
	prefix	: ['792', '793'],
	icon	: 'megafon-icon.png',
	imgclb	: 'opsosList.getCodeMegafonVolga',
	sendclb	: 'opsosList.sendMegafonVolga',
	error	: '<b>Ошибка!</b></font><br><br>'
};
opsoses[opsoses.length] = {
	id		: 'mts',
	name	: 'МТС',
	prefix	: ['7910', '7915', '7916', '7917', '7919', '7985', '7812', '7861', '7911', 
				'7912', '7913', '7914', '7918', '7980', '7981', '7982', '7983', '7987', '7988'],
	icon	: 'mts-icon.png',
	imgclb	: 'opsosList.getCodeMts',
	sendclb	: 'opsosList.sendMts',
	error	: '_lbError">'
};
opsoses[opsoses.length] = {
	id		: 'beeline',
	name	: 'Beeline',
	prefix	: ['7961', '7962', '7908', '7909', '7963', '7901', '7902', '7903', '7904',
				'7905', '7906', '7960', '7908', '7950', '7951', '7952', '7964'], 
	icon	: 'beeline-icon.png',
	imgclb	: 'opsosList.getCodeBeeline',
	sendclb	: 'opsosList.sendBeeline',
	error	: '<DIV style="margin-bottom: 20px" CLASS="err">'
};
opsoses[opsoses.length] = {
	id		: 'akos',
	name	: 'Akos',
	prefix	: ['7902', '7950'], 
	icon	: 'akos-icon.png',
	imgclb	: 'opsosList.getCodeAkos',
	sendclb	: 'opsosList.sendAkos',
	error	: 'class="no"'
};
opsoses[opsoses.length] = {
	id		: 'megafon-kavkaz',
	name	: 'Мегафон-Кавказ',
	prefix	: ['7928'], 
	icon	: 'megafon-icon.png',
	imgclb	: 'opsosList.getCodeMegafonKavkaz',
	sendclb	: 'opsosList.sendMegafonKavkaz',
	error	: '<td align="right" colspan="4">&nbsp;<font color="red">'
};

opsoses[opsoses.length] = {
	id		: 'kyivstar',
	name	: 'Киiвстар',
	prefix	: ['7067', '7098', '7097', '7096', '7039', '7068'], 
	icon	: 'kyivstar-icon.png',
	imgclb	: 'opsosList.getCodeDefault',
	sendclb	: 'opsosList.sendKyivstar',
	error	: '<p class="comment">'
};

opsoses[opsoses.length] = {
	id		: 'mtsua',
	name	: 'MTS UA',
	prefix	: ['38050', '38066', '38095', '38099'], 
	icon	: 'mtsua-icon.png',
	imgclb	: 'opsosList.getCodeMtsua',
	sendclb	: 'opsosList.sendMtsua',
	error	: '<b style="color: red;">'
};
/*
opsoses[opsoses.length] = {
	id		: 'megafon-centr',
	name	: 'Мегафон-Центр',
	prefix	: ['7920', '7929'], 
	icon	: 'megafon-icon.png',
	imgclb	: 'opsosList.getCodeMegafonCentr',
	sendclb	: 'opsosList.sendMegafonCentr',
	error	: 'err'
};
*/
opsoses[opsoses.length] = {
	id		: 'beelineua',
	name	: 'Beeline.ua',
	prefix	: ['38068'],
	icon	: 'beelineua-icon.png',
	imgclb	: 'opsosList.getCodeUkrbiznes',
	sendclb	: 'opsosList.sendUkrbiznes',
	error	: ''
};

opsoses[opsoses.length] = {
	id		: 'jeans',
	name	: 'Jeans',
	prefix	: ['38066', '38099'],
	icon	: 'beelineua-icon.png',
	imgclb	: 'opsosList.getCodeUkrbiznes',
	sendclb	: 'opsosList.sendUkrbiznes',
	error	: ''
};

opsoses[opsoses.length] = {
	id		: 'ncc-volga',
	name	: 'NCC Volga',
	prefix	: ['7908', '7904', '7950', '7951', '7902'],
	icon	: 'ncc-icon.png',
	imgclb	: 'opsosList.getCodeNcc',
	sendclb	: 'opsosList.sendNcc',
	error	: '<font class="errortext">'
};

function dbgLog(msg)
{
    if (["@mozilla.org/consoleservice;1"] in Components.classes)
    {
        Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService).logStringMessage(msg);
    }else
    {
        Application.console.log(msg);
    }
}


var trans = [];trans[0x401] = 0xA8;trans[0x451] = 0xB8;
for (var i = 0x410; i <= 0x44F; i++) trans[i] = i - 0x350;
		
var opsosList = {
	
	createListForSend : function () {
		sendSMS.disableFields();
		var menu = opsosList.$('sendsms-Opsos');
		var sel_name = document.createElement('menuitem');
		sel_name.setAttribute('id', '-1');
		sel_name.setAttribute('label', 'Select operator');
		sel_name.setAttribute('disabled', 'true');
		menu.appendChild(sel_name);
		opsosList.$('sendsms-OpsosList').selectedIndex = 0;
		menu.selectedIndex = 0;
		for (var opsos in opsoses) {
			var op_name = document.createElement('menuitem');
			op_name.setAttribute('id', opsoses[opsos].id);
			op_name.setAttribute('label', opsoses[opsos].name);
			op_name.setAttribute('disabled', 'true');
			menu.appendChild(op_name);
			for (var pref in opsoses[opsos]['prefix']) {
				var op_pref = document.createElement('menuitem');
				op_pref.setAttribute('id', opsoses[opsos].id);
				op_pref.setAttribute('label', '  +' + opsoses[opsos]['prefix'][pref]);
				op_pref.addEventListener("command", function (event) {
					opsosList.$("sendclb").setAttribute("value", opsosList.getSendCallback(this.id));
					document.operatorId = this.id;
					eval(opsosList.getImgCallback(this.id));
					}, false);
				menu.appendChild(op_pref);
			}
		}
		opsosList.createBook();
	},
	
	checkError: function (response) {
		for (var opsos in opsoses) {
			if (opsoses[opsos]['id'] == document.operatorId) {
				var err_str = '';
				var ok_str = '';
				if (opsoses[opsos].error)
					err_str = opsoses[opsos].error;
				else if(opsoses[opsos].ok)
					ok_str = opsoses[opsos].ok;
				break;
			}
		}
		if ((err_str && response.indexOf(err_str) != -1) || (ok_str && response.indexOf(ok_str) == -1))
		{
			alert("При отправке сообщения возникли проблемы. Попробуйте отправить сообщение позже");
			sendSMS.enableFields();
			sendSMS.loadCode();
			return false;
		} else {
			alert("Сообщение отправлено");
			sendSMS.clearFields();
			return true;
		}
	},
	
	send : function() {
		if (opsosList.$('sendclb').getAttribute('value'))
			eval(opsosList.$('sendclb').getAttribute('value'));
		else
			alert("Send callback is empty");
	},
	
	getImgCallback : function(id) {
		for (var opsos in opsoses) {
			if (opsoses[opsos]['id'] == id)
				return opsoses[opsos]['imgclb'] + "()"; 
		}
		return "opsosList.getCodeDefault()";
	},
	
	getSendCallback : function(id) {
		for (var opsos in opsoses) {
			if (opsoses[opsos]['id'] == id)
				return opsoses[opsos]['sendclb'] + "()"; 
		}
		alert("Send CallBack for " + id + " not found!");
		return false;
	},
	
	getCodeDefault : function() {
		sendSMS.enableFields();
		opsosList.$('op-key').src = "chrome://sendsms/skin/img/default.png";
		//document.getElementById('key').disabled = true;
	},
	
	getCodeMegafon : function() {
		sendSMS.enableFields();
		var code = Math.round(Math.random() * Math.random() * 1000000);
		opsosList.$('op-key').src = "http://megafonsib.ru/antirobot/?q=" + code;
		opsosList.$('op-code').value = code;
	},
	
	// Перекодировка из UTF в русскую
	myEscape : function(str) {
		var ret = [];
		for (var i = 0; i < str.length; i++) {
			var n = str.charCodeAt(i);
			if (typeof trans[n] != 'undefined') n = trans[n];
			if (n <= 0xFF) ret.push(n);
		}
		return escape(String.fromCharCode.apply(null, ret));
	},
	
	urlencode: function(str) {
	    var hexStr = function (dec) {
	        return '%' + (dec < 16 ? '0' : '') + dec.toString(16).toUpperCase();
	    };
	
	    var ret = '',
	            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
	    str = (str+'').toString();
	
	    for (var i = 0, dl = str.length; i < dl; i++) {
	        var ch = str.charAt(i);
	        if (unreserved.test(ch)) {
	            ret += ch;
	        }
	        else {
	            var code = str.charCodeAt(i);
	            if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters); https://developer.mozilla.org/index.php?title=en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt
	                ret += ((code - 0xD800) * 0x400) + (str.charCodeAt(i+1) - 0xDC00) + 0x10000;
	                i++; // skip the next one as we just retrieved it as a low surrogate
	            }
	            else if (code === 32) {
	                ret += '+'; // %20 in rawurlencode
	            }
	            else if (code < 128) { // 1 byte
	                ret += hexStr(code);
	            }
	            else if (code >= 128 && code < 2048) { // 2 bytes
	                ret += hexStr((code >> 6) | 0xC0);
	                ret += hexStr((code & 0x3F) | 0x80);
	            }
	            else if (code >= 2048) { // 3 bytes (code < 65536)
	                ret += hexStr((code >> 12) | 0xE0);
	                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
	                ret += hexStr((code & 0x3F) | 0x80);
	            }
	        }
	    }
	    return ret;
	},
	
	addZero : function(x) {return(x<0||x>9?0:"0")+x},

	
	sendMegafon : function() {
		this.key = opsosList.$('key').value;
		this.text = this.myEscape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.code = opsosList.$('op-code').value;
		
		if (!this.key || !this.text || !this.number || !this.code) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		var post = "";
		post += "tll=" + this.number;
		post += "&txt=" + this.text;
		post += "&keystring=" + this.key;
		post += "&smscode=" + this.code;
		var url = "http://sibir.megafon.ru/sendsms/";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url,true);
		xrequest.setRequestHeader("Referer","http://sibir.megafon.ru/sendsms/");
		xrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeTele2 : function() {
		var code = "76025"+Math.random();
		opsosList.$('op-code').value = code;
		var url = "http://www.sms.tele2.ru/get_rni.php?public_key="+code;
		opsosList.$('op-key').src = url;
		sendSMS.enableFields();
	},
	
	sendTele2 : function() {
		this.key = opsosList.$('key').value;
		this.text = opsosList.$('text').value;
		this.number = opsosList.$('number').value;
		this.code = opsosList.$('op-code').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(4);
		
		if (!this.key || !this.text || !this.number || !this.code) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		var post = "";
		post += "do=send";
		post += "&smstext=" + this.text;
		post += "&phone_code=" + this.pre;
		post += "&number=" + this.number;
		post += "&sms_text=" + this.text;
		post += "&private_key=" + this.key;
		post += "&public_key=" + this.code;
		post += "&submit=";

		var url = "http://www.sms.tele2.ru";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url,true);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeMegafonVolga : function ()
	{
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET","http://volga.megafon.ru/sms/includes/captcha.php",true);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 4:
					var url = "http://volga.megafon.ru/includes/sms_code_new.php?"+Math.random();
					opsosList.$('op-key').src = url;
					sendSMS.enableFields();
					break;
			}
		}
		
	},
	
	sendMegafonVolga : function()
	{
		this.key = opsosList.$('key').value;
		this.text = this.myEscape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.pre = parseInt(opsosList.$('sendsms-OpsosList').label);
		
		if (!this.key || !this.text || !this.number || !this.pre) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		var post = "";
		post += "flash=0&svalue=1&year=2009";
		post += "&pref=" + this.pre;
		post += "&Addr=" + this.number;
		post += "&smbody=" + escape(this.text);
		post += "&secretcode=" + this.key;
		
		var url = "http://volga.megafon.ru/sms/";
		var xrequest = new XMLHttpRequest();
		xrequest.open("POST",url,true);
		xrequest.setRequestHeader("Referer","http://volga.megafon.ru/sms/");
		xrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=windows-1251");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeMts : function()
	{
		var region = this.mts_region();
		var url = "http://www.mts.ru/live/smsxtra.aspx?region=" + region;
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf("LanapCaptcha.aspx", 0);
					var end = resp.indexOf("'", begin);
					var img = resp.substring(begin, end);
					begin = img.indexOf("amp;t=", 0);
					end = img.indexOf("&", begin);
					var code = img.substring(begin + 6, end);
					begin = resp.indexOf('id="__VIEWSTATE" value="', 0);
					end = resp.indexOf('"', begin + 24);
					mtsView = resp.substring(begin + 24, end);
					begin = resp.indexOf('id="__EVENTVALIDATION" value="', 0);
					end = resp.indexOf('"', begin + 30);
					mtsValid = resp.substring(begin + 30, end);
					opsosList.$('op-key').src = "http://www.mts.ru/live/" + img;
					opsosList.$('op-code').value = code;
					break;
			}
		};
		sendSMS.enableFields();
	},
	
	mts_region : function()
	{
		var pref = parseInt(opsosList.$('sendsms-OpsosList').label);
		var region = "msk";
		
		switch(pref)
		{
			case 7913:
			case 7383:
			case 7914:
				region = "nsk";
			break;
			case 7911:
				region = "spb";
			break;
			case 7917:
			case 7918:
			case 7919:
				region = "vladikavkaz";
			break;
			case 7988:
				region = "rnd";
			break;
			case 7910:
			case 7915:
			case 7919:
			case 7980:
				region = "kaluga";
			break;
			default:
				region = "msk";
			break;
		}
		return region;
	},
		
	sendMts : function()
	{
		this.key = opsosList.$('key').value;
		this.text = opsosList.$('text').value;
		this.number = opsosList.$('number').value;
		this.code = opsosList.$('op-code').value;
		
		if (!this.key || !this.text || !this.number || !this.code) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		var post = "";
		post += "&__EVENTTARGET=";
		post += "&__EVENTARGUMENT=";
		post += "&LBD_VCT_live_smsxtra_ctl00_ctl00_ctl00_smssendcontrol_captcha=" + this.code;
		post += "&__VIEWSTATE=" + this.urlencode(mtsView);
		post += "&__EVENTVALIDATION=" + this.urlencode(mtsValid);
		post += "&ctl00_ctl00_ctl00_SMSSendControl_RadFormDecorator1_ClientState=";
		post += "&ctl00$ctl00$ctl00$SMSSendControl$ddPrefix=913";
		post += "&ctl00$ctl00$ctl00$SMSSendControl$txtMsisdn=" + this.number;
		post += "&ctl00$ctl00$ctl00$SMSSendControl$tbMessage=" + this.text;
		post += "&ctl00$ctl00$ctl00$SMSSendControl$tbLen=" + this.text.length;
		post += "&ctl00$ctl00$ctl00$SMSSendControl$lblMax=135";
		post += "&ctl00$ctl00$ctl00$SMSSendControl$tbCode=" + this.key;
		post += "&ctl00$ctl00$ctl00$SMSSendControl$btnSend.x=70";
		post += "&ctl00$ctl00$ctl00$SMSSendControl$btnSend.y=9";
		post += "&ctl00$ctl09$ctl00$ctl01$ctl00$ctl01$ctl00$ctl00$ctl00$ctl01$ctl00$ctl00$ctl00$ctl00$ctl02$btnSend=?????????"
		
		var region = this.mts_region();

		var url = "http://www.mts.ru/live/smsxtra.aspx?region=" + region;
		dbgLog(url);
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					//dbgLog(xrequest.responseText);
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeBeeline : function()
	{
		var url = "http://www.beeline.ru/sms/index.wbp"
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf('NAME="confirm_key" VALUE="', 0);
					var end = resp.indexOf('"', begin + 26);
					var code = resp.substring(begin + 26, end);
					opsosList.$('op-key').src = "http://www.beeline.ru/mamimg.aspx";
					opsosList.$('op-code').value = code;
					break;
			}
		};
		sendSMS.enableFields();
	},
	
	sendBeeline : function()
	{
		this.key = opsosList.$('key').value;
		this.text = escape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(4);
		
		if (!this.key || !this.text || !this.number) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		var post = "";
		post += "send=";
		post += "&smstext=" + this.text;
		post += "&smstoprefix=" + this.pre;
		post += "&smsto=" + this.number;
		post += "&dirtysmstext=" + this.text;
		//post += "&translit=on";
		post += "&confirm_key=";
		post += "&confirmcode=" + this.key;
		post += "&x=60";
		post += "&y=3";

		var url = "http://www.beeline.ru/sms/index.wbp";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	sendKyivstar : function()
	{
		this.key = opsosList.$('key').value;
		this.text = escape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(4);
		
		if (!this.text || !this.number) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		var post = "";
		post += "submitted=true";
		post += "&lang=ua";
		post += "&sms_adv=502";
		post += "&mobcode=" + this.pre;
		post += "&smsto=" + this.number;
		post += "&lat=1"
		post += "&message=" + this.text;
		post += "&images_sid=82469863";
		post += "&live_images=010203"; // неправильно, надо сюда брать со страницы

		var url = "http://smsgate.kyivstar.ua/sms/?lang=ua";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", "http://smsgate.kyivstar.ua/sms/?lang=ua");
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeMtsua : function()
	{
		var url = "http://www.mts.com.ua/rus/sendsms.php"
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf('/back/modules/golden/captcha.php?', 0);
					var end = resp.indexOf('"', begin + 33);
					var img = resp.substring(begin, end);
					opsosList.$('op-key').src = "http://www.mts.com.ua/" + img;
					begin = resp.indexOf('sms_tag_id" value="');
					end = resp.indexOf('"', begin + 19);
					opsosList.$('op-code').value = resp.substring(begin+19, end);
					break;
			}
		};
		sendSMS.enableFields();
	},

	sendMtsua: function() {
		this.key = opsosList.$('key').value;
		this.text = escape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.code = opsosList.$('op-code').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(3);

		if (!this.key || !this.text || !this.number || !this.code || !this.pre) {
			alert("Нужно ввести все параметры.");
			return false;
		}

		var post = "";
		post += "script=/rus/sendsms.php";
		post += "&sms_tag_id=" + this.code;
		post += "&network1=" + this.pre;
		post += "&phone1=" + this.number;
		post += "&message=" + this.text;
		post += "&lang=lat";
		post += "&captcha=" + this.key;
		post += "&sender_name=";
		dbgLog(post);

		var url = "http://www.mts.com.ua/back/modules/sms/db_sms.php";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
		xrequest.send(post);

		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeMegafonCentr : function()
	{
		var url = "http://center.megafon.ru/center/sms_gate"
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf('name="PHPSESSID" value="', 0);
					var end = resp.indexOf('"', begin + 24);
					var phpsess = resp.substring(begin + 24, end);
					begin = resp.indexOf("name='id' value='", 0);
					end = resp.indexOf("'", begin + 17);
					var id = resp.substring(begin + 17, end);
					opsosList.$('op-key').src = "http://center.megafon.ru/info.php";
					opsosList.$('op-code').value = id;
					break;
			}
		}
		
		sendSMS.enableFields();
	},
	
	sendMegafonCentr : function() {
	},
	
	getCodeAkos : function() {
		var url = "http://akos.ru/cgi-bin/cgi.exe?function=sms_send&isFree=1"
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf('name="textTableRecordId" type="hidden" value="', 0);
					var end = resp.indexOf('"', begin + 46);
					var key1 = resp.substring(begin + 46, end);
					begin = resp.indexOf('/cgi-bin/cgi.exe?function=sms_show_antispam_image&ImageNumber=', 0);
					end = resp.indexOf('"', begin + 62);
					var image = resp.substring(begin, end);
					opsosList.$('op-key').src = "http://akos.ru/" + image;
					opsosList.$('op-code').value = key1;
					break;
			}
		}
		
		sendSMS.enableFields();
	},
	
	sendAkos : function() {
		this.key = opsosList.$('key').value;
		this.text = escape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.code = opsosList.$('op-code').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(4);
		
		if (!this.key || !this.text || !this.number || !this.code) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		
		var d = new Date();
		
		var post = "";
		post += "MMObjectType=0";
		post += "&isFree=1";
		post += "&MMObjectID=";
		post += "&Layer=";
		post += "&antispamText=" + this.key;
		post += "&textTableRecordId=" + this.code;
		post += "&To=" + this.urlencode("+7" + this.pre + this.number);
		post += "&Msg=" + this.text;
		post += "&count=" + parseInt(160 - opsosList.$('text').value.length);
		post += "&Day=" + this.addZero(d.getDate() + 1);
		post += "&Mon=" + this.addZero(d.getMonth() + 1);
		post += "&Year=" + ((d.getYear() < 2000) ? d.getYear() + 1900 : d.getYear());
		post += "&Hour=" + this.addZero(d.getHours());
		post += "&Min=" + this.addZero(d.getMinutes());
		post += "&DelayDay=";
		post += "&DelayMon=";
		post += "&DelayYear=";
		post += "&DelayHour=";
		post += "&DelayMin=";

		var url = "http://akos.ru/cgi-bin/cgi.exe?function=sms_send";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", "http://akos.ru/sms");
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	getCodeMegafonKavkaz : function() {
		var url = "http://kavkaz.megafon.ru/sms"
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf('src="svg-images/', 0);
					var end = resp.indexOf('"', begin + 5);
					var image = resp.substring(begin + 5, end);
					opsosList.$('op-key').src = "http://kavkaz.megafon.ru/" + image;
					break;
			}
		}
		sendSMS.enableFields();
	},
	
	sendMegafonKavkaz : function() {
		this.key = opsosList.$('key').value;
		this.text = escape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(4);
		
		if (!this.key || !this.text || !this.number) {
			alert("Нужно ввести все параметры.");
			return false;
		}
		
		var post = "";
		post += "&phone_number=" + this.urlencode(this.pre + this.number);
		post += "&msg=" + this.text;
		post += "&sender=";
		post += "&to_translit=";
		post += "&day=";
		post += "&month=";
		post += "&hours=";
		post += "&minutes=";
		post += "&passwd=" + this.key;		

		var url = "http://kavkaz.megafon.ru/sms";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xrequest.send(post);
		
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},

	getCodeUkrbiznes : function() {
//		var url = "http://77.247.216.53/cap.php"
//		opsosList.$('op-key').src = url;
		sendSMS.enableFields();
		document.getElementById('key').disabled			= true;
		document.getElementById('op-key').src = "chrome://sendsms/skin/img/default.png";
	},

	sendUkrbiznes: function() {
		this.text =  this.myEscape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(3);

		var op = '';
		//check operator
		switch(this.pre) {
			case '38050':
				op = 'mts50';
				break;
			case '38095':
				op = 'mts95';
				break;
			case '38066':
				op = 'jeans66';
				break;
			case '38099':
				op = 'jeans99';
				break;
			case '38067':
				op = 'kyivstar67';
				break;
			case '38096':
				op = 'kyivstar96';
				break;
			case '38097':
				op = 'kyivstar97';
				break;
			case '38098':
				op = 'kyivstar98';
				break;
			case '38098':
				op = 'kyivstar98';
				break;
			case '38068':
				op = 'beeline';
				break;
		}

		if (!this.text || !this.number || !op) {
			alert("Нужно ввести все параметры.");
			return false;
		}

		var post = "";
		post += "&operator=" + op;
		post += "&fff=" + this.pre;
		post += "&phone=" + this.number;
		post += "&textsms=" + this.text;
		post += "&sub=Отправить";
		dbgLog(post);
		var url = "http://ukrbiznes.com/infocenter/index.php?topic=7&option=1";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xrequest.send(post);

		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					//dbgLog(xrequest.responseText);
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},

	getCodeNcc : function() {
		var url = "http://nnov.ncc-volga.ru/services/sms/"
		var xrequest = new XMLHttpRequest();
		xrequest.open("GET",url);
		xrequest.send("");
		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 4:
					var resp = xrequest.responseText;
					var begin = resp.indexOf('id="captcha_sid" value="', 0);
					var end = resp.indexOf('"', begin + 24);
					var code = resp.substring(begin + 24, end);
					opsosList.$('op-code').value = code;
					opsosList.$('op-key').src = "http://nnov.ncc-volga.ru/bitrix/tools/captcha.php?captcha_sid=" + code;
					break;
			}
		}
		sendSMS.enableFields();
	},

	sendNcc : function() {
		this.key = opsosList.$('key').value;
		this.code = opsosList.$('op-code').value;
		this.text = escape(opsosList.$('text').value);
		this.number = opsosList.$('number').value;
		this.pre = new String(opsosList.$('sendsms-OpsosList').label).substr(4);

		if (!this.key || !this.text || !this.number || !this.code) {
			alert("Нужно ввести все параметры.");
			return false;
		}

		var post = "";
		post += "&code=" + this.pre;
		post += "&number=" + this.number;
		post += "&text=" + this.text;
		post += "&captcha_sid=" + this.code;
		post += "&captcha=" + this.key;

		var url = "http://nnov.ncc-volga.ru/services/sms/?action=send";
		var xrequest = XMLHttpRequest();
		xrequest.open("POST",url);
		xrequest.setRequestHeader("Referer", url);
		xrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xrequest.send(post);

		xrequest.onreadystatechange = function() {
			switch(xrequest.readyState) {
				case 2:
					sendSMS.disableFields();
					break;
				case 3:
					break;
				case 4:
					opsosList.checkError(xrequest.responseText);
					break;
				default:
					alert("oops: " + xrequest.readyState);
					break;
			}
		}
	},
	
	Cc: this.Components.classes,
	Ci: this.Components.interfaces,
	
	$: function(name) {
		return document.getElementById(name);
	},
	
	createBook : function(text, number) {
	    var today = new Date();
	    //get extensions file path
//	    const id = "{591d7ab0-fbf4-4b82-99af-606797c8c396}";
//	    var extension = opsosList.Cc["@mozilla.org/extensions/manager;1"].getService(opsosList.Ci.nsIExtensionManager).getInstallLocation(id).getItemLocation(id);
		var profile_dir = Components.classes["@mozilla.org/file/directory_service;1"].
                     getService(Components.interfaces.nsIProperties).
                     get("ProfD", Components.interfaces.nsIFile);  
	    // check for write permission
	    try {
	        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	    } catch (e) {
	        dbgLog("Permission to write to file was denied.");
	    }
//	    //create proper path for xml file
	    var DS = ((profile_dir.path.search(/\\/) != -1) ? "\\" : "/");
	    var theFile = profile_dir.path + DS + "sendsms_book.xml";
//	    //create component for file writing
	    var file = opsosList.Cc["@mozilla.org/file/local;1"].createInstance(opsosList.Ci.nsILocalFile);
	    file.initWithPath( theFile );
	    try {
			if (!file.exists()) {
				file.create( opsosList.Ci.nsIFile.NORMAL_FILE_TYPE, 420);
				var xmlDoc = '<?xml version="1.0" encoding="utf-8" ?>';
//				create file output stream and use write/create/truncate mode
				var stream = opsosList.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(opsosList.Ci.nsIFileOutputStream);
				stream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
				stream.write(xmlDoc, xmlDoc.length);
				stream.close();
			}
	    } catch(e) {
	        dbgLog("error file create");
	    }
	}
	

	
}
